package com.qding.park.manager.web.exceptionHandler;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.IExceptionHandle;
import com.qding.park.common.core.exceptions.auth.AuthException;
import com.qding.park.common.core.exceptions.business.validator.RchlValidatorException;
import com.qding.park.manager.common.rest.domain.AnswerMsg;
import com.qding.park.manager.common.rest.domain.AnswerMsgFactoryImpl;
import com.qding.park.manager.common.rest.enums.ResponseCodeEnum;
import com.qding.park.manager.web.controller.Web_CarController;

@RestController
@ControllerAdvice
public class GlobalExceptionHandler {
	private static final Logger _log = LoggerFactory.getLogger(Web_CarController.class);
	
	@Autowired
	public IExceptionHandle exceptionHandle;
	
	
	
	@ExceptionHandler(value = RchlValidatorException.class)
	@ResponseBody
	public Object validatorErrorHandler(HttpServletRequest req, Exception e) throws Exception {
		RchlValidatorException error=(RchlValidatorException) e;
		_log.error("---validatorErrorHandler Handler---Host {} invokes url {} ERROR: {}", req.getRemoteHost(),
				req.getRequestURL(), error.getExceptionMessage().getDetailText());
		exceptionHandle.handle(e);
		AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
				error.getExceptionMessage().getDetailText());
		return answerMsg;
	}
	
	
	@ExceptionHandler(value = AuthException.class)
	@ResponseBody
	public Object authErrorHandler(HttpServletRequest req, Exception e) throws Exception {
		_log.error("---AuthErrorHandler Handler---Host {} invokes url {} ERROR: {}", req.getRemoteHost(),
				req.getRequestURL(), e.getMessage());
		exceptionHandle.handle(e);
		AuthException error=(AuthException) e;
		AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.PERMISSION_ERROR.getValue(),
				error.getExceptionMessage().getSimpleText());
		return answerMsg;
	}
	
	@ExceptionHandler(value = BaseException.class)
	@ResponseBody
	public Object baseErrorHandler(HttpServletRequest req, Exception e) throws Exception {
		_log.error("---BaseException Handler---Host {} invokes url {} ERROR: {}", req.getRemoteHost(),
				req.getRequestURL(), e.getMessage());
		exceptionHandle.handle(e);
		BaseException error=(BaseException) e;
		AnswerMsg answerMsg = AnswerMsgFactoryImpl.createSuccessAnserMsg(error.getExceptionMessage().getDetailText());
		return answerMsg;
	}
	
	
	
	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public Object defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
		_log.error("---DefaultException Handler---Host {} invokes url {} ERROR: {}", req.getRemoteHost(),
				req.getRequestURL(), e.getMessage());
		
		exceptionHandle.handle(e);
		AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
		return answerMsg;
	}

}
